const { isArray } = require('baidu-aip-sdk/src/util/objectTools')
const fs = require('fs')
class baiduMiddleWare {
  async getImageInfo(ctx, next) {
    // const { buffer } = ctx.req.file
    // const image = buffer.toString('base64')
    if (isArray(ctx.request.files.image)) {
      const images = ctx.request.files.image.map(item => fs.readFileSync(item.path, { encoding: 'base64' }))
      const imageType = 'BASE64'
      ctx.request.img = {
        images,
        imageType,
      }
    } else {
      const image = fs.readFileSync(ctx.request.files.image.path, {encoding:'base64'})
      const imageType = 'BASE64'
      const groupId = 'group1'
      ctx.request.img = {
        image,
        imageType,
        groupId,
      }
    }
    
    await next()
  }
}

module.exports = new baiduMiddleWare()
